package com.tdd;

import org.I0Itec.zkclient.ZkClient;

/**
 * @author tudedong
 * @description
 * @date 2020-07-11 12:34:20
 */
public abstract class AbstractTemplateLock implements Lock{

    @Override
    public void getLock() {
        //获取锁
        if(tryLock()){
            System.out.println(Thread.currentThread().getName()+"，获取锁成功，，，");
        }else{
            //开始等待锁，事件监听
            waitLock();
            //重新获取锁
            getLock();
        }
    }

    /**
     * 获取锁
     * @return
     */
    protected abstract boolean tryLock();

    /**
     * 等待锁
     */
    protected abstract void waitLock();

    /**
     * 释放锁
     */
    protected abstract void unLockImpl();

    @Override
    public void unLock() {
        unLockImpl();
    }
}
